package com.example.securitydemo.po;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Package: com.example.securitydemo.po
 * @ClassName: AnotherUser
 * @Author: Think
 * @CreateTime: 2021/8/1 15:10
 * @Description:
 */
@Data
public class AnotherUser2 implements UserDetails{

    private Integer userId;

    private String userName;

    private String password;

    private Integer expired;

    private Integer locked;

    private List<Resource> resourceList;

    /**
     * 获取用户的所有角色信息
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        for(Resource resource : resourceList){
            authorities.add(new SimpleGrantedAuthority(resource.getResourceCode()));
        }
        return authorities;
    }

    /**
     * 指定哪一个是用户的密码字段
     * @return
     */
    @Override
    public String getPassword() {
        return password;
    }

    /**
     * 指定哪一个是用户的账户字段
     * @return
     */
    @Override
    public String getUsername() {
        return userName;
    }


    /**
     * 判断账户是否过期
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        return (expired == 0);
    }

    /**
     * 判断账户是否锁定
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return (locked == 0);
    }

    /**
     * 判断密码是否过期
     * 可以根据业务逻辑或者数据库字段来决定
     * @return
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 判断账户是否可用
     * 可以根据业务逻辑或者数据库字段来决定
     * @return
     */
    @Override
    public boolean isEnabled() {
        return true;
    }

}
